﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YArchitech.Controls
{
	public class YJKTextBox : TextBox
	{
		public string OldValue
		{
			get
			{
				return this.oldValue;
			}
		}

		[Description("是否只输入数字")]
		[Category("盈建科数据设置")]
		public InputSelection IsNumber
		{
			get
			{
				return this.isNumber;
			}
			set
			{
				this.isNumber = value;
				if (this.isNumber == InputSelection.character)
				{
					this.isPositive = SymbolSelect.none;
					this.isInt = NumberType.none;
					this.mostValue.Maximum = 0.0;
					this.mostValue.Minimal = 0.0;
					base.ImeMode = ImeMode.NoControl;
					this.isRemoveZero = false;
					return;
				}
				base.ImeMode = ImeMode.Disable;
				this.prefix = "";
			}
		}

		[Description("是正数还是负数")]
		[DefaultValue("none")]
		[Category("盈建科数据设置")]
		public SymbolSelect IsPositive
		{
			get
			{
				return this.isPositive;
			}
			set
			{
				this.isPositive = value;
				if (this.isPositive != SymbolSelect.none)
				{
					this.IsNumber = InputSelection.number;
				}
			}
		}

		[Description("是整型还是小数")]
		[DefaultValue("none")]
		[Category("盈建科数据设置")]
		public NumberType IsInt
		{
			get
			{
				return this.isInt;
			}
			set
			{
				this.isInt = value;
				if (this.isInt != NumberType.none)
				{
					this.IsNumber = InputSelection.number;
				}
				if (this.isInt != NumberType.doubles)
				{
					this.efficacious = -1;
				}
			}
		}

		[Description("设置最小值-最大值")]
		[RefreshProperties(RefreshProperties.All)]
		[Category("盈建科数据设置")]
		public MostValue MostValue
		{
			get
			{
				return this.mostValue;
			}
			set
			{
				this.mostValue = value;
				if (this.mostValue.Maximum != 0.0 || this.mostValue.Minimal != 0.0)
				{
					this.prefix = "";
					this.IsNumber = InputSelection.number;
				}
			}
		}

		[Description("设置前缀")]
		[RefreshProperties(RefreshProperties.All)]
		[Category("盈建科数据设置")]
		public string Prefix
		{
			get
			{
				return this.prefix;
			}
			set
			{
				this.prefix = value;
				if (this.prefix != null && this.prefix.Trim() != "")
				{
					this.IsNumber = InputSelection.character;
					this.mostValue.Maximum = 0.0;
					this.mostValue.Minimal = 0.0;
					this.Text = this.prefix;
				}
			}
		}

		[Description("是否可为空")]
		[Category("盈建科数据设置")]
		public bool IsEmpty
		{
			get
			{
				return this.isEmpty;
			}
			set
			{
				this.isEmpty = value;
			}
		}

		[Description("是否自动清除多余的0，只在设置为数字时有效")]
		[Category("盈建科数据设置")]
		public bool IsRemoveZero
		{
			get
			{
				return this.isRemoveZero;
			}
			set
			{
				this.isRemoveZero = value;
				if (this.isRemoveZero && this.IsNumber != InputSelection.number)
				{
					this.IsNumber = InputSelection.number;
				}
			}
		}

		[Description("设置小数点后的位数，只在设置为double时有效")]
		[Category("盈建科数据设置")]
		public int Efficacious
		{
			get
			{
				return this.efficacious;
			}
			set
			{
				this.efficacious = value;
				if (this.efficacious >= 0 && this.isInt != NumberType.doubles)
				{
					this.IsInt = NumberType.doubles;
				}
			}
		}

		[Description("检查在光标离开后，如果内容为空，是否恢复原来内容")]
		[Category("盈建科数据设置")]
		public bool IsRecord
		{
			get
			{
				return this.isRecord;
			}
			set
			{
				this.isRecord = value;
			}
		}

		[Description("是否可以输入算术符号'+'")]
		[Category("盈建科数据设置")]
		public bool IsInputPlus
		{
			get
			{
				return this.isInputPlus;
			}
			set
			{
				this.isInputPlus = value;
			}
		}

		[Description("是否可以输入算术符号'-'")]
		[Category("盈建科数据设置")]
		public bool IsInputMinus
		{
			get
			{
				return this.isInputMinus;
			}
			set
			{
				this.isInputMinus = value;
			}
		}

		protected override void OnGotFocus(EventArgs e)
		{
			base.OnGotFocus(e);
			this.oldValue = this.Text;
		}

		protected override void OnLeave(EventArgs e)
		{
			if (this.isRecord)
			{
				if (this.Text.Trim() == "")
				{
					this.Text = this.oldValue;
				}
			}
			else if (!this.IsEmpty && this.Text.Trim() == "")
			{
				MessageBox.Show("参数不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				base.Focus();
			}
			if (this.isNumber == InputSelection.number && this.Text.Trim() != "")
			{
				if (this.Text.Length == 1 && (this.Text == "+" || this.Text == "-" || this.Text == "."))
				{
					this.Text = "";
					base.Focus();
				}
				if (this.isRemoveZero)
				{
					this.Text = StringTools.RemoveZero(this.Text);
				}
				if (this.isInt == NumberType.doubles && this.efficacious >= 0)
				{
					this.Text = StringTools.CutOffEfficacious(this.Text, this.efficacious);
				}
			}
			base.OnLeave(e);
		}

		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			this.textFlag = false;
			if (this.isNumber == InputSelection.number)
			{
				if (StringTools.IsAlphabet(e.KeyChar))
				{
					this.textFlag = true;
					e.Handled = true;
				}
				if (e.KeyChar == '.' && this.Text.Contains("."))
				{
					this.textFlag = true;
					e.Handled = true;
				}
			}
			if (!this.isInputPlus)
			{
				if (e.KeyChar == '+')
				{
					this.textFlag = true;
					e.Handled = true;
				}
			}
			else if (e.KeyChar == '+' && this.Text.Contains("+"))
			{
				this.textFlag = true;
				e.Handled = true;
			}
			if (!this.isInputMinus)
			{
				if (e.KeyChar == '-')
				{
					this.textFlag = true;
					e.Handled = true;
				}
			}
			else if (e.KeyChar == '-' && this.Text.Contains("-"))
			{
				this.textFlag = true;
				e.Handled = true;
			}
			base.OnKeyPress(e);
		}

		protected override void OnTextChanged(EventArgs e)
		{
			if (this.textFlag)
			{
				this.textFlag = false;
				return;
			}
			if (this.isNumber == InputSelection.number && this.Text != null)
			{
				try
				{
					double num = Convert.ToDouble(this.Text.Trim());
					if (num > 2147483647.0 || num < -2147483647.0)
					{
						return;
					}
				}
				catch (Exception)
				{
					return;
				}
			}
			base.OnTextChanged(e);
			if (this.isNumber == InputSelection.number && this.Text != null)
			{
				if (this.GetRegex().IsMatch(this.Text))
				{
					this.oldText = this.Text;
					if (this.hasHandled)
					{
						this.start = base.SelectionStart;
					}
					else
					{
						base.SelectionStart = this.start;
					}
					this.hasHandled = true;
				}
				else
				{
					this.Text = this.oldText;
					this.hasHandled = false;
				}
				this.MostValueCheck();
				return;
			}
			if (this.isNumber == InputSelection.character && this.Text != null && this.Text != null && this.prefix != null && this.Text.Length > this.prefix.Length)
			{
				if (this.Text.Substring(0, this.prefix.Length) != this.prefix)
				{
					if (this.prefixHandled)
					{
						this.start = base.SelectionStart;
						this.prefixHandled = false;
					}
					this.Text = this.prefix + this.Text;
					base.SelectionStart = this.start + this.prefix.Length;
					return;
				}
				this.prefixHandled = true;
			}
		}

		private Regex GetRegex()
		{
			Regex result = null;
			if (this.isInt == NumberType.doubles || this.isInt == NumberType.none)
			{
				if (this.isPositive == SymbolSelect.positive)
				{
					result = new Regex(this.regexpositive);
				}
				else if (SymbolSelect.negative == this.isPositive)
				{
					result = new Regex(this.regexnegative);
				}
				else if (this.isPositive == SymbolSelect.none)
				{
					result = new Regex(this.regex);
				}
			}
			else if (this.isInt == NumberType.integer)
			{
				if (this.isPositive == SymbolSelect.positive)
				{
					result = new Regex(this.regexintpositive);
				}
				else if (SymbolSelect.negative == this.isPositive)
				{
					result = new Regex(this.regexintnegative);
				}
				else if (this.isPositive == SymbolSelect.none)
				{
					result = new Regex(this.regexint);
				}
			}
			return result;
		}

		private void MostValueCheck()
		{
			if (this.mostValue.Maximum != 0.0 || this.mostValue.Minimal != 0.0)
			{
				try
				{
					double num = Convert.ToDouble(this.Text.Trim());
					if (num > this.mostValue.Maximum || num < this.mostValue.Minimal)
					{
						this.ForeColor = System.Drawing.Color.Red;
					}
					else
					{
						this.ForeColor = Color.Black;
					}
				}
				catch (Exception)
				{
				}
			}
		}

		private string regex = "^(\\+|\\-|\\d){0,1}\\d*\\.{0,1}\\d*$";

		private string regexint = "^(\\+|\\-|\\d){0,1}\\d*\\d*$";

		private string regexpositive = "^(\\+|\\d){0,1}\\d*\\.{0,1}\\d*$";

		private string regexintpositive = "^(\\+|\\d){0,1}\\d*\\d*$";

		private string regexnegative = "^\\-\\d*\\.{0,1}\\d*$";

		private string regexintnegative = "^\\-\\d*\\d*$";

		private string oldText = "";

		private int start;

		private bool hasHandled = true;

		private bool prefixHandled = true;

		private string oldValue = "0";

		private InputSelection isNumber;

		private SymbolSelect isPositive;

		private NumberType isInt;

		private MostValue mostValue = new MostValue(0.0, 0.0);

		private string prefix;

		private bool isEmpty = true;

		private bool isRemoveZero;

		private int efficacious = -1;

		private bool textFlag;

		private bool isRecord;

		private bool isInputPlus;

		private bool isInputMinus = true;
	}
}
